package com.ruoyi.sales.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.ZcProcess;
import com.ruoyi.common.core.domain.entity.ZcStock;
import com.ruoyi.sales.domain.ZcSpecifications;
import com.ruoyi.sales.service.IZcProcessService;
import com.ruoyi.sales.service.IZcSpecificationsService;
import com.ruoyi.sales.service.IZcStockService;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sales.domain.ZcProduct;
import com.ruoyi.sales.service.IZcProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品Controller
 * 
 * @author woo
 * @date 2024-04-02
 */
@RestController
@RequestMapping("/sales/product")
public class ZcProductController extends BaseController
{
    @Autowired
    private IZcProductService zcProductService;

    @Autowired
    private IZcStockService zcStockService;

    @Autowired
    private IZcProcessService zcProcessService;

    @Autowired
    private IZcSpecificationsService zcSpecificationsService;

    @Autowired
    private ISysDictDataService sysDictDataService;

    /**
     * 查询产品列表
     */
    @PreAuthorize("@ss.hasPermi('sales:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZcProduct zcProduct)
    {
        startPage();
        List<ZcProduct> list = zcProductService.selectZcProductList(zcProduct);
        for (ZcProduct product : list) {
            product.setStock(zcStockService.selectStockById(product.getStockId()));
            product.setProcess(zcProcessService.selectProcessById(product.getProcessId()));
        }
        return getDataTable(list);
    }

    /**
     * 导出产品列表
     */
    @PreAuthorize("@ss.hasPermi('sales:product:export')")
    @Log(title = "产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZcProduct zcProduct)
    {
        List<ZcProduct> list = zcProductService.selectZcProductList(zcProduct);
        ExcelUtil<ZcProduct> util = new ExcelUtil<ZcProduct>(ZcProduct.class);
        util.exportExcel(response, list, "产品数据");
    }

    /**
     * 获取产品详细信息
     */
    @PreAuthorize("@ss.hasPermi('sales:product:query')")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId)
    {
        return success(zcProductService.selectZcProductByProductId(productId));
    }

    /**
     * 新增产品
     */
    @PreAuthorize("@ss.hasPermi('sales:product:add')")
    @Log(title = "产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZcProduct zcProduct)
    {
        return toAjax(zcProductService.insertZcProduct(zcProduct));
    }

    /**
     * 修改产品
     */
    @PreAuthorize("@ss.hasPermi('sales:product:edit')")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZcProduct zcProduct)
    {
        return toAjax(zcProductService.updateZcProduct(zcProduct));
    }

    /**
     * 删除产品
     */
    @PreAuthorize("@ss.hasPermi('sales:product:remove')")
    @Log(title = "产品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {
        return toAjax(zcProductService.deleteZcProductByProductIds(productIds));
    }

    /**
     * 获取原料品类树列表
     */
    @PreAuthorize("@ss.hasPermi('sales:product:list')")
    @GetMapping("/stockTree")
    public AjaxResult stockTree(ZcStock stock)
    {
        return success(zcStockService.selectStockTreeList(stock));
    }


    /**
     * 获取类别工序树列表
     */
    @PreAuthorize("@ss.hasPermi('sales:product:list')")
    @GetMapping("/processTree")
    public AjaxResult processTree(ZcProcess process)
    {
        return success(zcProcessService.selectProcessTreeList(process));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('sales:product:edit')")
    @Log(title = "计划编码", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody ZcProduct zcProduct)
    {
        return toAjax(zcProductService.updateCodecStatus(zcProduct));
    }

    /**
     * 获取规格列表
     */
    @PreAuthorize("@ss.hasPermi('sales:product:list')")
    @GetMapping("/specList")
    public AjaxResult specList(){
        List<ZcSpecifications> zcSpecifications = zcSpecificationsService.selectAll();
        for (ZcSpecifications specification : zcSpecifications) {
            String label = sysDictDataService.selectDictLabel("zc_specifications", specification.getWide().toString());
            specification.setWide(Long.valueOf(label));
        }
        return success(zcSpecifications);
    }

}
